import db from "../models/index.js";
let userFriendGroup = db.user_friend_group;
let userFriendGroupDao = {};
//判断是否存在这个用户好友或者群
userFriendGroupDao.isExisted = (_account, _friend_account, _session_id, _type) => {
    if(_type) {
        return userFriendGroup.findOne({
            where: {
                _account,
                _session_id,
                _type
            },
            raw: true,
        });
    }
    return userFriendGroup.findOne({
        where: {
            _account,
            _friend_account,
            _type
        },
        raw: true,
    });
};
//判断好友sessionID是否已经存在
userFriendGroupDao.isExistedSessionID = (_session_id) => {
    return userFriendGroup.findOne({
        where: {
            _session_id
        },
        raw: true,
    });
};
//获取我的好友和群列表 或者获取群成员
userFriendGroupDao.getUserFriendGroupMember = (_account, _session_id) => {
    if(_session_id) {
        return userFriendGroup.findAndCountAll({
            where: {
                _session_id
            },
            raw:true
        });
    }
    return userFriendGroup.findAndCountAll({
        where: {
            _account
        },
        raw:true

    });
};
//添加好友或者群
userFriendGroupDao.addUserFriendGroup = (_account, _friend_account, _session_id, _type, _remark, t) => {
    if(!t) {
        return userFriendGroup.create({_account, _friend_account, _session_id, _type, _remark});
    }
    return userFriendGroup.create({_account, _friend_account, _session_id, _type, _remark}, {transaction:t});
};
//更新好友的某条信息
userFriendGroupDao.updateUserFriendGroup = (_account, _friend_account, _session_id, _type, _remark, t) => {
    if(!t) {
        return userFriendGroup.update({_friend_account, _session_id, _type, _remark}, {where:{_account}});
    }
    return userFriendGroup.update({_friend_account, _session_id, _type, _remark}, {where:{_account}, transaction:t});
};
//删除我的好友
userFriendGroupDao.deleteUserFriend = (_account, _friend_account, _session_id, _type, t) => {
    if(!t) {
        return userFriendGroup.destroy({where:{_account, _friend_account, _session_id, _type}});
    }
    return userFriendGroup.destroy({where:{_account, _friend_account, _session_id, _type}}, {transaction:t});
};
//删除同一sessionid 解散群
userFriendGroupDao.deleteUserGroup = (_session_id, t) => {
    if(!t) {
        return userFriendGroup.destroy({where:{_session_id}});
    }
    return userFriendGroup.destroy({where:{_session_id}}, {transaction:t});
};
export default userFriendGroupDao;
